package cn.telling.product.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import cn.telling.product.dao.IProductPricePoliceDao;
import cn.telling.product.vo.ProductPublishVo;
import cn.telling.utils.BaseUtil;

/**
 * 
 * @ClassName: ProductPricePoliceDaoImpl 
 * @Description: 产品价格政策数据服务接口的实现
 * @author 陈赟 
 * @date 2015年6月16日 下午4:18:02 
 *
 */
@Repository("productPricePoliceDao")
public class ProductPricePoliceDaoImpl implements IProductPricePoliceDao {
    
    private static Logger LOGGER = LoggerFactory.getLogger(ProductPricePoliceDaoImpl.class);
    
    @Autowired
    @Qualifier("film-template")
    private JdbcTemplate jdbcTemplate;

    @Override
    public ProductPublishVo QueryProductPublish(String areaId, String shopProId) {
        String month = new SimpleDateFormat("yyyy-MM").format(new Date());
        String day = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        
        StringBuilder sb = new StringBuilder();
        sb.append("select pp.id,pp.productid,pp.policyarea,a.areaname,pp.startdate,pp.enddate,pp.price,pp.add_date ");
        sb.append(" from product_pricepolice pp left join policystatus pys on pp.id = pys.policyId ");
        sb.append(" left join shop_product sp on pp.productid = sp.productid ");
        sb.append(" left join areainfo a on a.areaid = pp.policyarea ");
        sb.append(" where pys.status = 1  and sp.id=? and (pp.policyarea = 2 or pp.policyarea=?) ");
        sb.append(" and pp.startdate >= to_date(?,'YYYY-MM') and pp.enddate <= add_months(to_date(?,'YYYY-MM'),1) ");
        sb.append(" and pp.startdate <= to_date(?,'YYYY-MM-DD') and pp.enddate >= to_date(?,'YYYY-MM-DD') ");
        sb.append(" order by pp.add_date desc");
        
        String sql = sb.toString();
        LOGGER.debug("查询产品的价格政策：" + BaseUtil.logSQL(sql, new Object[]{shopProId,areaId,month,month,day,day}));
        List<ProductPublishVo> list = jdbcTemplate.query(sql, new Object[]{shopProId,areaId,month,month,day,day}, new RowMapper<ProductPublishVo>(){

            @Override
            public ProductPublishVo mapRow(ResultSet rs, int rowNum)
                    throws SQLException {
                ProductPublishVo productPublishVO = new ProductPublishVo();
                productPublishVO.setPricePoliceId(rs.getString("id"));
                productPublishVO.setProductId(rs.getString("productid"));
                productPublishVO.setPolicyArea(rs.getString("policyarea"));
                productPublishVO.setPolicyName(rs.getString("areaname"));
                productPublishVO.setStartDate(rs.getTimestamp("startdate"));
                productPublishVO.setEndDate(rs.getTimestamp("enddate"));
                productPublishVO.setPrice(rs.getBigDecimal("price"));
                productPublishVO.setAdd_date(rs.getTimestamp("add_date"));
                return productPublishVO;
            }
            
        });
        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }

}
